package com.core;

import org.apache.commons.configuration.Configuration;

import com.utility.PropertiesConjunction;

import java.util.Random;
//import java.lang.StringBuffer;

public class RollMethod {
	
	public static enum Method {
		Default, Dummy, Numerical, AllChars, Custom;
	}
	
	private Configuration configuration = PropertiesConjunction.getCoreConfiguration();
	
	final private String DEFAULT_RESULT = configuration.getString("com.core.roll.dummyvalue", "0");
	final private int DEFAULT_DIGIT = configuration.getInt("com.core.roll.defaultdigit", 5);
	
	
	public String Roll(int digit, String validset) {
		return Roll(Method.Custom, digit, validset);
	}
	
	public String Roll(Method method, int digit, String validset) {
		if (method == null) {
			method = Method.Default;
		}
		
		if (digit <= 0) {
			digit = DEFAULT_DIGIT;
		}
		
		switch (method) {
			case Dummy:
				return DEFAULT_RESULT;
				
			case Default:
				validset = configuration.getString("com.core.roll.default", "01");
				break;
				
			case Numerical:
				validset = configuration.getString("com.core.roll.numerical", "0123456789");
				break;
				
			case AllChars:
				validset = configuration.getString(	"com.core.roll.allchars",
													"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
				break;
				
			case Custom:
				break;
			
			default:
				break;
			
		}
		
		return doRoll(digit, validset);
	}
	
	
	
	
	public String doRoll(int digit, String validset) {
		char [] validsetChar = validset.toCharArray();
		int length = validset.length();
		Random random = new Random();
		StringBuffer buff = new StringBuffer();
		
		if (digit <= 0) {
			digit = DEFAULT_DIGIT;
		}
		
		while (digit > 0) {
			buff.append(validsetChar[random.nextInt(length)]);
			digit--;
			
		}
		
		return buff.toString();
		
	}
	
}
